from database.db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import json

index_data = {}

class IndexData:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        
    def add_data(self, data_key, data_code, data_name, data_type):
        if data_key.isalnum():
            data_key = data_key.lower()
        if data_key in index_data.keys():
            if len(index_data[data_key][0]) < 8:
                if data_code not in index_data[data_key][0]:
                    index_data[data_key][0].append(data_code)
                    index_data[data_key][1].append(data_name)
                    index_data[data_key][2].append(data_type)
        else:
            index_data[data_key] = []
            index_data[data_key].append([])
            index_data[data_key].append([])
            index_data[data_key].append([])
            index_data[data_key][0].append(data_code)
            index_data[data_key][1].append(data_name)
            index_data[data_key][2].append(data_type)
            
    def query_gupiao_data(self, sql, data_type):
        #sql = "select Fcode,Fname from entity_gupiao;"
        print(sql)
        self.gupiao_data = self.db.query_sql(sql)
        for gupiao in self.gupiao_data:
            #print(gupiao[0] + str(len(gupiao[0])) + gupiao[1] + str(len(gupiao[1])))
            for i in range (0,len(gupiao[0])):
                for j in range (i + 1,len(gupiao[0]) + 1):
                    #print(gupiao[0][i:j])
                    self.add_data(gupiao[0][i:j], gupiao[0], gupiao[1], data_type)
            for i in range (0,len(gupiao[1])):
                for j in range (i + 1,len(gupiao[1]) + 1):
                    #print(gupiao[1][i:j])
                    self.add_data(gupiao[1][i:j], gupiao[0], gupiao[1], data_type)
    
    
    def process_name(self, name):
        self.need_split = 0
        new_name = name.replace("&", "").replace("(", "").replace(")", "").replace(".", "").replace("-", "")
        #简单用空格判断,碰到复杂的再调整
        if new_name.find(" ") > 1:
            self.need_split = 1
            return new_name.lower()
        return new_name
    
    def query_gudong_data(self, sql, data_type):
        #sql = "select Fcode,Fname from entity_gupiao;"
        print(sql)
        gudong_data = self.db.query_sql(sql)
        for gudong in gudong_data:
            new_name = self.process_name(gudong[1])
            if self.need_split == 1:
                names = new_name.split(" ")
                for name in names:
                    self.add_data(name, gudong[0], gudong[1], data_type)
            else:
                for i in range (0,len(new_name)):
                    for j in range (i + 1,len(new_name) + 1):
                        #print(gudong[1][i:j])
                        self.add_data(new_name[i:j], gudong[0], gudong[1], data_type)
    
    def write_file_data(self):
        print("write_file_data")
        file = open("./out/index_data.sql", "w", encoding="utf8")
        for data_key in index_data.keys():
            for i in range(0, len(index_data[data_key][0])):
                try:
                    sql = "insert into entity_index(Findex_word, Findex_code, Findex_name, Findex_tag, Findex_type, Fcreate_time, Fmodify_time) values "
                    sql += "('" + data_key +  "','" + index_data[data_key][0][i] + "', '" + index_data[data_key][1][i] + "', " + str(i) + "," + str(index_data[data_key][2][i]) + ", now(), now());"
                    #print(sql)
                    #file.write(sql)
                    self.db.execute_not_safe(sql)
                except Exception as e:
                    update_sql = "update entity_index set Fmodify_time = now()"
                    update_sql += ",Findex_code='" + index_data[data_key][0][i] + "',Findex_name='" + index_data[data_key][1][i] + "',Findex_type=" + str(index_data[data_key][2][i])
                    update_sql += " where Findex_word='" + data_key + "' and Findex_tag = " + str(i) + ";\n"
                    #print(update_sql)
                    #file.write(update_sql)
                    self.db.execute_not_safe(update_sql)
                    
            '''
            try:
                sql = "insert into entity_index (Findex_word, Fcreate_time, Fmodify_time) values ('" + data_key + "', now(), now());\n"
                #print(sql)
                file.write(sql)
                #self.db.execute_not_safe(sql)
            except Exception as e:
                print(str(e))
                pass
            update_sql = "update entity_index set Fmodify_time = now()"
            update_sql += ",Findex_code1='" + index_data[data_key][0][0] + "',Findex_name1='" + index_data[data_key][1][0] + "',Findex_type1=" + str(index_data[data_key][2][0])
            if len(index_data[data_key][0]) > 1:
                update_sql += ",Findex_code2='" + index_data[data_key][0][1] + "',Findex_name2='" + index_data[data_key][1][1] + "',Findex_type2=" + str(index_data[data_key][2][1])
            if len(index_data[data_key][0]) > 2:
                update_sql += ",Findex_code3='" + index_data[data_key][0][2] + "',Findex_name3='" + index_data[data_key][1][2] + "',Findex_type3=" + str(index_data[data_key][2][2])
            if len(index_data[data_key][0]) > 3:
                update_sql += ",Findex_code4='" + index_data[data_key][0][3] + "',Findex_name4='" + index_data[data_key][1][3] + "',Findex_type4=" + str(index_data[data_key][2][3])
            if len(index_data[data_key][0]) > 4:
                update_sql += ",Findex_code5='" + index_data[data_key][0][4] + "',Findex_name5='" + index_data[data_key][1][4] + "',Findex_type5=" + str(index_data[data_key][2][4])
            if len(index_data[data_key][0]) > 5:
                update_sql += ",Findex_code6='" + index_data[data_key][0][5] + "',Findex_name6='" + index_data[data_key][1][5] + "',Findex_type6=" + str(index_data[data_key][2][5])
            if len(index_data[data_key][0]) > 6:
                update_sql += ",Findex_code7='" + index_data[data_key][0][6] + "',Findex_name7='" + index_data[data_key][1][6] + "',Findex_type7=" + str(index_data[data_key][2][6])
            if len(index_data[data_key][0]) > 7:
                update_sql += ",Findex_code8='" + index_data[data_key][0][7] + "',Findex_name8='" + index_data[data_key][1][7] + "',Findex_type8=" + str(index_data[data_key][2][7])
            update_sql += " where Findex_word='" + data_key + "';\n"
            #print(update_sql)
            file.write(update_sql)
            '''
        file.close()    

if __name__ == '__main__':
    code = IndexData()
    code.query_gupiao_data("select Fcode,Fname from entity_gupiao;", 0)
    #code.query_gupiao_data("select Fcode,Fname from entity_jijin;", 1)
    #十大股东中包含英文机构，需要单独进行分词处理
    #code.query_gudong_data("select Fcode,Fname from entity_gudong;", 2)
    code.write_file_data()